package org.ihelpuoo.weboo.module;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import org.ihelpuoo.I;
import org.ihelpuoo.lang.StringUtils;

public class ModuleMusterFile
{
	private static final String	LOADER_BEGIN	= "::Loader List::";

	private List<String>		loaders;
	private String				licenceInfo;

	public ModuleMusterFile(File file) throws IOException
	{
		this(new FileInputStream(file));
	}

	private ModuleMusterFile(InputStream inStream) throws IOException
	{
		loaders = new LinkedList<String>();
		licenceInfo = null;
		BufferedReader br = new BufferedReader(new InputStreamReader(inStream));
		String s;
		StringBuffer sb = new StringBuffer();
		while ((s = br.readLine()) != null)
		{
			s = StringUtils.trim(s);
			if (s.startsWith(LOADER_BEGIN))
				licenceInfo = sb.toString();
			else if (licenceInfo == null)
			{
				sb.append(I.NEW_LINE);
				sb.append(s);
			}
			else
			{
				if (StringUtils.isBlank(s))
					continue;
				else if (s.startsWith("#"))
					continue;
				else
					loaders.add(s);
			}
		}
	}

	public int size()
	{
		return loaders.size();
	}

	public String getLicenceInfo()
	{
		return licenceInfo;
	}

	public Iterator<String> iterator()
	{
		return loaders.iterator();
	}

}
